
using System;
using System.Text;
using LastFMGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using LastFMGenNHibernate.EN.LastFM;

namespace LastFMGenNHibernate.CAD.LastFM
{
        public partial class EventoCAD : BasicCAD, IEventoCAD
        {
        public EventoCAD() : base ()
        {
        }

        public EventoCAD(ISession sessionAux) : base (sessionAux)
        {
        }



        public EventoEN ReadOIDDefault (int id)
        {
                EventoEN eventoEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        eventoEN = (EventoEN)session.Load (typeof(EventoEN),
                                id

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in EventoCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return eventoEN;
        }


        public int New_ (EventoEN evento)
        {
                try
                {
                        SesssionInitializeTransaction ();

                        session.Save (evento);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in EventoCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return evento.Id;
        }

        public System.Collections.Generic.IList<EventoEN> DevolverEventos (int first, int size)
        {
                System.Collections.Generic.IList<EventoEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        if (size > 0)
                                result = session.CreateCriteria (typeof(EventoEN)).
                                         SetFirstResult (first).SetMaxResults (size).List<EventoEN>();
                        else
                                result = session.CreateCriteria (typeof(EventoEN)).List<EventoEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in EventoCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }

        public void Modify (EventoEN evento)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        EventoEN eventoEN = (EventoEN)session.Load (typeof(EventoEN), evento.Id);

                        eventoEN.Nombre = evento.Nombre;


                        eventoEN.Descripcion = evento.Descripcion;

                        session.Update (eventoEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in EventoCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        }
}
